<?php

class CEPController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    $form = new Application_Form_CDR();
    	$form->submit->setLabel('Search');
    	
    	$this->view->form = $form;
    	 
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    		if ($form->isValid($formData)) {
    			
    			$cep_start_date = $form->getValue('cep_start_date');
    			$cep_end_date = $form->getValue('cep_end_date');
    			
    			$params = null;
    
    			    			
    			if ($cep_start_date != null){
    				if ($params == null) {
    					$params = array('cep_start_date' => $cep_start_date);
    				} else {
    					$params = array_merge($params, array('cep_start_date' => $cep_start_date));
    				}
    			}
    			
    			if ($cep_end_date != null){
    				if ($params == null) {
    					$params = array('cep_end_date' => $cep_end_date);
    				} else {
    					$params = array_merge($params, array('cep_end_date' => $cep_end_date));
    				}
    			}
    			if ($params != null) {
    				$this->_helper->redirector('index', 'cep', null, $params);
    			} else {
    				$this->_helper->redirector('index', 'cep');
    			}
    
    		} else {
    			$form->populate($formData);
    		}
    	}//end of ispost

    	 else {
    		$request = $this->getRequest();
    		
    		$cep_start_date = $request->getParam('cep_start_date');
    		$cep_end_date = $request->getParam('cep_end_date');
    		$prod_no = $request->getParam('prod_no');
    		$show =  $request->getParam('show_detail');
    		$this->view->show = $show;
    		$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    		    			    		'host'     => 'amun1.eng.uts.edu.au',
    		    			    	    'port'	   => '5006',
    		    			    		'username' => 'test',
    		    			    		'password' => '1234',
    		    			    		'dbname'   => 'tops'
    		));
    		$db ->setFetchMode(Zend_Db::FETCH_OBJ);
    		$product = new Application_Model_DbTable_Products();
    		 
    		if($show != "yes"){
    			if($cep_start_date != Null && $cep_end_date == Null)
    			{
    			
    				$selectCeps = $db->select()
    						->from(array('c'=>'ceps'),
    								array('cep_no', 'cep_date', 'cep_description', 'staff_no', 'prod_no', 'cust_no')
    								)
    						->join(array('d'=>'departments'), ('c.dep_no = d.dep_no'), 
    									array('dep_name')
    								)
    						->where('cep_date >= ?', $cep_start_date);
    			 
    				$this->view->ceps = $db->fetchAll($selectCeps);
    	   			
    			} 
    			elseif($cep_start_date == Null && $cep_end_date != Null) 
    			{
    				$selectCeps = $db->select()
    							->from(array('c'=>'ceps'),
    									array('cep_no', 'cep_date', 'cep_description', 'staff_no', 'prod_no', 'cust_no')
    								)
    							->join(array('d'=>'departments'), ('c.dep_no = d.dep_no'),
    								array('dep_name')
    								)
    							->where('cep_date <= ? ', $cep_end_date);
    			
    				$this->view->ceps = $db->fetchAll($selectCeps);
    			
    			} 
    			elseif($cep_start_date != Null && $cep_end_date!=Null) 
    			{
    			
    			
    				$selectCeps = $db->select()
    							->from(array('c'=>'ceps'),
    									array('cep_no', 'cep_date', 'cep_description', 'staff_no', 'prod_no', 'cust_no')
    								)
    							->join(array('d'=>'departments'), ('c.dep_no = d.dep_no'),
    								array('dep_name')
    								)
    				->where("cep_date >= ?", $cep_start_date)
    				->where("cep_date <= ?", $cep_end_date);
    				$this->view->ceps = $db->fetchAll($selectCeps);
    			} 
    			else 
    			{
    			
    			}
    		
    		}// end of what if show is yes
    		else 
    		{
    			if($cep_start_date != Null && $cep_end_date == Null)
    			{
    				 
    				$selectCeps = $db->select()
    				->from(array('c'=>'ceps'),
    				array('cep_no', 'cep_date', 'cep_description', 'staff_no', 'prod_no', 'cust_no')
    				)
    				->join(array('d'=>'departments'), ('c.dep_no = d.dep_no'),
    				array('dep_name')
    				)
    				->where('cep_date >= ?', $cep_start_date);
    			
    				
    				
    				$selectProducts = $db->select()
    				->from(array('c'=>'ceps'),
    				array('cep_no', 'cep_date', 'cep_description', 'staff_no', 'prod_no', 'cust_no')
    				)
    				->join(array('d'=>'departments'), ('c.dep_no = d.dep_no'),
    				array('dep_name')
    				)
    				->where('cep_date >= ?', $cep_start_date)
    				->where('prod_no = ?', $prod_no);
    				 
    				$this->view->products = $db->fetchAll($selectProducts);
    				$this->view->ceps = $db->fetchAll($selectCeps);
    				 
    			}
    			elseif($cep_start_date == Null && $cep_end_date != Null)
    			{
    				$selectCeps = $db->select()
    				->from(array('c'=>'ceps'),
    				array('cep_no', 'cep_date', 'cep_description', 'staff_no', 'prod_no', 'cust_no')
    				)
    				->join(array('d'=>'departments'), ('c.dep_no = d.dep_no'),
    				array('dep_name')
    				)
    				->where('cep_date <= ? ', $cep_end_date);
    				 
    				
    				$selectProducts = $db->select()
    				->from(array('c'=>'ceps'),
    				array('cep_no', 'cep_date', 'cep_description', 'staff_no', 'prod_no', 'cust_no')
    				)
    				->join(array('d'=>'departments'), ('c.dep_no = d.dep_no'),
    				array('dep_name')
    				)
    				->where('cep_date <= ? ', $cep_end_date)
    				->where('prod_no = ?', $prod_no);
    					
    				$this->view->products = $db->fetchAll($selectProducts);
    				$this->view->ceps = $db->fetchAll($selectCeps);
    				 
    			}
    			elseif($cep_start_date != Null && $cep_end_date!=Null)
    			{
    				 
    				 
    				$selectCeps = $db->select()
    				->from(array('c'=>'ceps'),
    				array('cep_no', 'cep_date', 'cep_description', 'staff_no', 'prod_no', 'cust_no')
    				)
    				->join(array('d'=>'departments'), ('c.dep_no = d.dep_no'),
    				array('dep_name')
    				)
    				->where("cep_date >= ?", $cep_start_date)
    				->where("cep_date <= ?", $cep_end_date);
    				
    				$selectProducts = $db->select()
    				->from(array('c'=>'ceps'),
    				array('cep_no', 'cep_date', 'cep_description', 'staff_no', 'prod_no', 'cust_no')
    				)
    				->join(array('d'=>'departments'), ('c.dep_no = d.dep_no'),
    				array('dep_name')
    				)
    				->where("cep_date >= ?", $cep_start_date)
    				->where("cep_date <= ?", $cep_end_date)
    				->where('prod_no = ?', $prod_no);
    				$this->view->products = $db->fetchAll($selectProducts);
    				$this->view->ceps = $db->fetchAll($selectCeps);
    			}
    			else
    			{
    				/*foreach($result as $index):
    				$no = $index->item_no;
    				$items->deleteItem($no);
    				endforeach;*/
    			}
    		}
    		
    	}
    }
    
    public function reviewAction()
    {
        $ceps = new Application_Model_DbTable_CEPs();
        $this->view->ceps = $ceps->fetchAll();
        
    }
        public function addAction()
    {
        
        $request = $this->getRequest();
        $prod_no2 = $this->_getParam('prod_no');
        $prod_qty = $this->_getParam('prod_quantity');
        $dep = $request->getParam('dep_no');
        
        $auth = Zend_Auth::getInstance();
        $staff_no = $auth->getIdentity()->staff_no;
        
        
        
        
        $form = new Application_Form_CEP();
        $this->view->form = $form;
        $form->staff_no->setValue($staff_no);
        $form->prod_no->setValue($prod_no2);
        if ($prod_qty == 0) {
        	$form->cep_description->setValue('Out of Stock');
		}
        if($dep != null)
        {
        	$form->dep_no->setValue($dep);
        }
        
        $date = new Zend_Date();
        $date = $date->get('yyyy-MM-dd');

        if ($this->getRequest()->isPost()) {
        	$formData = $this->getRequest()->getPost();
        	if ($form->isValid($formData)) {
        		
        		$cep_date = $date;
        		$cep_description = $form->getValue('cep_description');
        		$prod_no = $form->getValue('prod_no');
        		$staff_no =$form->getValue('staff_no');
        		$cust_no = $form->getValue('cust_no');
        		$dep_no = $form->getValue('dep_no');
        		$prod_description = $form->getValue('prod_description');
        		
        		$ceps = new Application_Model_DbTable_CEPs();
        		$ceps->addCEP($cep_date, $cep_description,  $staff_no, $prod_no, $prod_description, $cust_no, $dep_no);
        		
        		$this->_helper->redirector('review', 'cep');
        	} else {
        		$form->populate($formData);
        	}
        }
        
        
    }

    public function deleteAction()
    {
   	 	if($this->getRequest()->isPost())
        {
        	$del = $this->getRequest()->getPost('del');
        	if($del=='Yes')
        	{
        		$cep_no = $this->getRequest()->getPost('cep_no');
        		$ceps = new Application_Model_DbTable_CEPs();
        		$ceps->deleteCEP($cep_no);
           	}
        	$this->_helper->redirector('index', 'index');
        }
        else 
        {
        	$cep_no = $this->_getParam('cep_no', 0);
			$ceps = new Application_Model_DbTable_CEPs();
			$this->view->cep = $ceps->getCEP($cep_no);
        }
    }

}





